package cn.wolfcode.edu.web.controller;

import cn.wolfcode.edu.domain.Role;
import cn.wolfcode.edu.query.QueryObject;
import cn.wolfcode.edu.service.IRoleService;
import cn.wolfcode.edu.util.JsonResult;
import cn.wolfcode.edu.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by abb on 2018-07-09.
 */
@RequestMapping("role")
@Controller
public class RoleController {
    @Autowired
    private IRoleService roleService;

    //定位 角色页面
    @RequestMapping("")
    public String index() {
        return "role";
    }

    //角色数据列表
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo) {
        return roleService.query(qo);
    }

    //添加角色
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Role role) {
        JsonResult jsonResult = new JsonResult();

        try {
            roleService.insert(role);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResult.setMsg("get wrong");
        }
        return jsonResult;
    }
    //编辑角色
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Role role) {
        JsonResult jsonResult = new JsonResult();

        try {
            roleService.updateByPrimaryKey(role);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResult.setMsg("get wrong");
        }
        return jsonResult;
    }
    //查询所有的角色
    @RequestMapping("listAll")
    @ResponseBody
    public List<Role> listAll() {
        return roleService.selectAll();
    }

    //根据员工的ID查询角色的 id集合
    @RequestMapping("listIdsByEmployeeId")
    @ResponseBody
    public List<Long> listIdsByEmployeeId(Long employeeId) {
        return roleService.listIdsByEmployeeId(employeeId);
    }
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try{
            roleService.deleteByPrimaryKey(id);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("有错误");
        }
        return result;
    }
}
